package com.myappengine.dao;

import javax.jdo.PersistenceManager;
import javax.jdo.Transaction;

import org.springframework.stereotype.Repository;

import com.google.appengine.api.datastore.KeyFactory;
import com.myappengine.model.UploadedFile;

@Repository
public class UploadedFileDAOImpl implements UploadedFileDAO {

	@Override
	public void deleteUploadedFile(String uploadedFileKey) {
		PersistenceManager pm = PMF.getPersistenceManager().getPersistenceManager();
		Transaction transaction = null;
		try {
			transaction = pm.currentTransaction();
			transaction.begin();
			UploadedFile file = pm.getObjectById(UploadedFile.class, KeyFactory.stringToKey(uploadedFileKey));
			pm.deletePersistent(file);
			transaction.commit();
		} finally {
			if (transaction.isActive()) {
				transaction.rollback();
			}
			pm.close();
		}
	}
}
